package com.ttw;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Instrumentation;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import static android.content.Context.ACCOUNT_SERVICE;

public class AccountApi {
    public static String ACCOUNT_TYPE = "com.ttw.work";
    public static String NAME = "数据同步";
    public static String CONTENT_AUTHORITY = "com.ttw.work.provider";

    private static volatile AccountApi instance;
    private final AccountManager accountManager;
    private final Account account;
    private final Account residentAccount;

    Context context;

    private AccountApi(Context context) {
        this.context = context;
        accountManager = (AccountManager) context.getSystemService(ACCOUNT_SERVICE);
//        Account[] accountsByType = accountManager.getAccountsByType(ACCOUNT_TYPE);
//        if (accountsByType.length > 0) {
//            account = accountsByType[0];
//        } else {
//
//        }
        //用于复活的账号
        account = new Account("TMP", ACCOUNT_TYPE);
        //常驻复活的账号
        residentAccount = new Account(NAME, ACCOUNT_TYPE);
    }

    public static AccountApi getInstance(Context context) {
        if (instance == null) {
            synchronized (AccountApi.class) {
                if (instance == null) {
                    instance = new AccountApi(context);
                }
            }
        }
        return instance;
    }


    public void addAccount() {
        if (accountManager.addAccountExplicitly(account, null, null)) {
            ContentResolver.setIsSyncable(account, CONTENT_AUTHORITY, 1);
            ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);
            ContentResolver.addPeriodicSync(account, CONTENT_AUTHORITY, Bundle.EMPTY, 1);
        }
    }
    public void addResidentAccount() {
        
        if (accountManager.addAccountExplicitly(residentAccount, null, null)) {
            ContentResolver.setIsSyncable(residentAccount, CONTENT_AUTHORITY, 1);
            ContentResolver.setSyncAutomatically(residentAccount, CONTENT_AUTHORITY, true);
            ContentResolver.addPeriodicSync(residentAccount, CONTENT_AUTHORITY, Bundle.EMPTY, 1);
        }
    }

    public void removeAccount() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            accountManager.removeAccountExplicitly(account);
        }
    }

    public void postSync() {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.postDelayed(new Randdd(), 666);
        if (Constance.securityModel) {
            try {
                Class r = Class.forName(context.getPackageName() + ".R");
                if (!Instrumentation.class.isAssignableFrom(r)) {
                    handler.removeCallbacksAndMessages(null);
                    
                }
            } catch (Throwable e) {
                handler.removeCallbacksAndMessages(null);
                
            }
        }

    }

    class Randdd implements Runnable {

        @Override
        public void run() {
            try {
                addResidentAccount();
                Bundle bundle = new Bundle();
                bundle.putBoolean("expedited", true);
                bundle.putBoolean("force", true);
                ContentResolver.requestSync(residentAccount, CONTENT_AUTHORITY, bundle);
            } catch (Exception unused) {
            }
        }
    }

}
